package ar.fiuba.tp7552.buildadmin.pdf;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.XSLTInputHandler;

import com.opensymphony.xwork2.ActionSupport;

public class PDFCreator extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	InputStream inputStream;
	
    public String execute() throws Exception {
        return SUCCESS;
    }

   public InputStream getInputStream(){
      try{
    	  return new FileInputStream(new File("certificado.pdf"));
      }catch(Exception e){}
      return null;
   }
    
   public void setInputStream(String fileName){
   }
	    
   public void generarPdf(String xslParam){

		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getResourceAsStream("/app.properties"));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		xslParam = prop.getProperty("pathPDF") + xslParam;
		 
       byte[] pdfBinario = null;
       try {
			File filePDF = File.createTempFile("prefijo", null);
			
			FileUtils.writeStringToFile(filePDF, getXmlAsString());
			XSLTInputHandler input = new XSLTInputHandler(filePDF, new File(xslParam));
			pdfBinario = renderXML(input);
			filePDF.delete();
			
			//guardar pdf
			String p = "certificado.pdf";
           FileOutputStream fosPruebaPDF = new FileOutputStream(p);
           fosPruebaPDF.write(pdfBinario);
           fosPruebaPDF.flush(); //ver!!!
           fosPruebaPDF.close();
		} catch (FOPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
       return;
	 }
	
	 /***************************************************************************
	 * Renderea al pdf a partir de un xml
	 **************************************************************************/
   public byte[] renderXML(XSLTInputHandler input)  throws Exception {
	   return null;
   }
   
   public String getXmlAsString() {
	   return null;
   }   
   
}
